-- Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[p_WM_ThanhKhoan_HoSo_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_Insert]

IF OBJECT_ID(N'[dbo].[p_WM_ThanhKhoan_HoSo_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_Update]

IF OBJECT_ID(N'[dbo].[p_WM_ThanhKhoan_HoSo_InsertUpdate]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_InsertUpdate]

IF OBJECT_ID(N'[dbo].[p_WM_ThanhKhoan_HoSo_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_Delete]

IF OBJECT_ID(N'[dbo].[p_WM_ThanhKhoan_HoSo_DeleteDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_DeleteDynamic]

IF OBJECT_ID(N'[dbo].[p_WM_ThanhKhoan_HoSo_Load]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_Load]

IF OBJECT_ID(N'[dbo].[p_WM_ThanhKhoan_HoSo_SelectDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_SelectDynamic]

IF OBJECT_ID(N'[dbo].[p_WM_ThanhKhoan_HoSo_SelectAll]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_SelectAll]


GO


------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_ThanhKhoan_HoSo_Insert]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_Insert]
	@LanThanhKhoan bigint,
	@TuNgay datetime,
	@DenNgay datetime,
	@TongDoanhThu decimal(18, 0),
	@GhiChu nvarchar(max),
	@ID bigint OUTPUT
AS

INSERT INTO [dbo].[t_WM_ThanhKhoan_HoSo]
(
	[LanThanhKhoan],
	[TuNgay],
	[DenNgay],
	[TongDoanhThu],
	[GhiChu]
)
VALUES 
(
	@LanThanhKhoan,
	@TuNgay,
	@DenNgay,
	@TongDoanhThu,
	@GhiChu
)

SET @ID = SCOPE_IDENTITY()

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_ThanhKhoan_HoSo_Update]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_Update]
	@ID bigint,
	@LanThanhKhoan bigint,
	@TuNgay datetime,
	@DenNgay datetime,
	@TongDoanhThu decimal(18, 0),
	@GhiChu nvarchar(max)
AS

UPDATE
	[dbo].[t_WM_ThanhKhoan_HoSo]
SET
	[LanThanhKhoan] = @LanThanhKhoan,
	[TuNgay] = @TuNgay,
	[DenNgay] = @DenNgay,
	[TongDoanhThu] = @TongDoanhThu,
	[GhiChu] = @GhiChu
WHERE
	[ID] = @ID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_ThanhKhoan_HoSo_InsertUpdate]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_InsertUpdate]
	@ID bigint,
	@LanThanhKhoan bigint,
	@TuNgay datetime,
	@DenNgay datetime,
	@TongDoanhThu decimal(18, 0),
	@GhiChu nvarchar(max)
AS
IF EXISTS(SELECT [ID] FROM [dbo].[t_WM_ThanhKhoan_HoSo] WHERE [ID] = @ID)
	BEGIN
		UPDATE
			[dbo].[t_WM_ThanhKhoan_HoSo] 
		SET
			[LanThanhKhoan] = @LanThanhKhoan,
			[TuNgay] = @TuNgay,
			[DenNgay] = @DenNgay,
			[TongDoanhThu] = @TongDoanhThu,
			[GhiChu] = @GhiChu
		WHERE
			[ID] = @ID
	END
ELSE
	BEGIN
		
		INSERT INTO [dbo].[t_WM_ThanhKhoan_HoSo]
		(
			[LanThanhKhoan],
			[TuNgay],
			[DenNgay],
			[TongDoanhThu],
			[GhiChu]
		)
		VALUES 
		(
			@LanThanhKhoan,
			@TuNgay,
			@DenNgay,
			@TongDoanhThu,
			@GhiChu
		)		
	END
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_ThanhKhoan_HoSo_Delete]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_Delete]
	@ID bigint
AS

DELETE FROM 
	[dbo].[t_WM_ThanhKhoan_HoSo]
WHERE
	[ID] = @ID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_ThanhKhoan_HoSo_DeleteDynamic]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_DeleteDynamic]
	@WhereCondition NVARCHAR(500)
AS

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 'DELETE FROM [dbo].[t_WM_ThanhKhoan_HoSo] WHERE ' + @WhereCondition

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_ThanhKhoan_HoSo_Load]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_Load]
	@ID bigint
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[LanThanhKhoan],
	[TuNgay],
	[DenNgay],
	[TongDoanhThu],
	[GhiChu]
FROM
	[dbo].[t_WM_ThanhKhoan_HoSo]
WHERE
	[ID] = @ID
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_ThanhKhoan_HoSo_SelectDynamic]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_SelectDynamic]
	@WhereCondition NVARCHAR(500),
	@OrderByExpression NVARCHAR(250) = NULL
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 
'SELECT 
	[ID],
	[LanThanhKhoan],
	[TuNgay],
	[DenNgay],
	[TongDoanhThu],
	[GhiChu]
FROM [dbo].[t_WM_ThanhKhoan_HoSo] 
WHERE ' + @WhereCondition

IF @OrderByExpression IS NOT NULL AND LEN(@OrderByExpression) > 0
BEGIN
	SET @SQL = @SQL + ' ORDER BY ' + @OrderByExpression
END

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_ThanhKhoan_HoSo_SelectAll]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_ThanhKhoan_HoSo_SelectAll]
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[LanThanhKhoan],
	[TuNgay],
	[DenNgay],
	[TongDoanhThu],
	[GhiChu]
FROM
	[dbo].[t_WM_ThanhKhoan_HoSo]	

GO

